$(function() {
    MyArticleList();
});

// 我的文章里获得文章列表
function MyArticleList() {
    var userid = localStorage.user_id || sessionStorage.user_id;
    var token = localStorage.token || sessionStorage.token;

    if (token){
        $.ajax({
            url: host + "/my_article/" + userid ,
            method: "get",
            // data: params,
            headers: {'Authorization': 'JWT ' + token},
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                // location.href = './my_article.html';
                var case_list = eval(resp);
                console.log(case_list)
                console.log(resp)
                html = '';
                for (var i in case_list){


                    html +=
                        "<tr>" +
                            "<td style='text-align: center'><a href="  + "../html/article_detail.html?" + case_list[i].id + ">" + case_list[i].article_name + "</a></td>" +
                            "<td style='text-align: center'>" + case_list[i].article_classify + "</td>" +
                            "<td style='text-align: center'>" + case_list[i].update_time.substring(0,16) + "</td>" +
                            "<td style='text-align: center'>" +
                                "<button onclick='' type='button' class='ccc btn btn-default dropdown-toggle my_article_btn'" + "value=" + case_list[i].id + ">修改</button>"+
                                "<button onclick='ArticleDelete(this)' type='button' class='btn btn-default dropdown-toggle my_article_btn'" + "value=" + case_list[i].id + ">删除</button>"+
                            "</td>" +
                        "</tr>"
                }
                html1 =
                    "<tr>" +
                        "<th style='text-align: center'>标题</th>" +
                        "<th style='text-align: center'>文章分类</th>" +
                        "<th style='text-align: center'>发布时间</th>" +
                        "<th style='text-align: center'>操作</th>" +
                    "</tr>";
                $('#my_article_list').html(html1 + html)

            },
            error: function (resp) {
                alert('服务器异常,请重试！')
            }
        })
    }
}


// 我的文章里删除文章
function ArticleDelete(e) {
    var token = localStorage.token || sessionStorage.token;
    var article_id = $(e).val();
    var result = confirm('确定删除?');
    if (result){
        $.ajax({
            url: host + "/delete_article/" + article_id ,
            method: "delete",
            // data: params,
            headers: {'Authorization': 'JWT ' + token},
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                $(e).parent().parent().remove()

            },
            error: function (resp) {
                alert('服务器异常,请重试！')
            }
        })
    }
}

// 我的文章里修改文章